package com.tansun.easycare.rule.common;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.tansun.easycare.rule.common.jfun.JFun;
import com.tansun.easycare.rule.common.model.Bom;
import com.tansun.easycare.rule.common.model.Domain;
import com.tansun.easycare.rule.datamodel.entity.RuleDataDomain;
import com.tansun.easycare.rule.datamodel.entity.RuleDataparams;
import com.tansun.easycare.rule.datamodel.entity.RuleDataset;
import com.tansun.easycare.rule.referencedata.entity.RuleReferenceData;
import com.tansun.easycare.rule.rulegroup.model.RuleRefVO;
import com.tansun.easycare.rule.rulegroup.model.RuleSetVO;
import com.tansun.easycare.rule.ruleproj.entity.RuleAuthority;
import com.tansun.easycare.rule.ruleproj.entity.RuleProject;
import com.tansun.easycare.rule.ruleproj.entity.RuleProjectRank;
import com.tansun.rule.utils.MClass;

/**
 * 数据缓存,根据Project
 * @author xch
 *
 */
public class DataCache {
	public static final String SPLIT = "-@-";
	public static final String JFUNCACHE_OPTION = "jFunCache";
	public static final String BOMCACHE_OPTION = "bomCache";
	public static final String DOMAINCACHE_OPTION = "domianCache";
	public static final String DOMAINMAP_OPTION = "domainMap";
	public static final String RULEPARSEDRL_OPTION = "ruleParseDrl";//drl解析
	public static final String DATASETTREEVO_OPTION = "dataSetTreeVO";
	public static final String MAPRULEDATASET_OPTION = "mapRuleDataSet";
	public static final String MCLASSCACHE_OPTION = "MClassCache";
	public static final String RULEDATAPARAMS_MAP_OPTION = "ruleDataparams_MAP";//按照projectCode查找的全部数据模型参数缓存，值为map形式。key为className+'.'+paramName
	public static final String RULEDATAPARAMS_LIST_OPTION = "ruleDataparamsS_LIST";//按照projectCode查找的全部数据模型参数缓存，值map形式。key为dataSetId
	public static final String REFFUNCPARAM_OPTION = "refFuncParam";//按照projecCode查找的全部函数变量缓存，值为collection形式
	public static final String RULESETEFFECT_OPTION = "ruleSetEffect";//按照project查询出的有效规则组织成，关联规则缓存
	public static final String RULE_REF_ENTRY_GROUP_OPTION = "ruleRefEntryGroup";//规则关联的条目及规则集缓存
	public static final String RULE_AUTH_MAP_OPTION = "ruleAuthority_Map";//权限缓存
	public static final String RULE_PRO_LIST_OPTION = "ruleProjectList";//全部项目缓存
	public static final String RULE_PRO_RANK_MAP_OPTION = "ruleProjectRankList";//全部项目目录缓存
	
	
	/**
	 * 缓存
	 */	
	public static Map<String,Map<String, Bom>> BOMCACHE = new ConcurrentHashMap<String,Map<String, Bom>>();
	public static Map<String,Map<String, Domain>> DOMAINCACHE = new ConcurrentHashMap<String,Map<String, Domain>>();
	public static Map<String,List<RuleDataDomain>> DOMAINMAP = null; 
	public static Map<String,List<JFun>> JFUNCACHE = new ConcurrentHashMap<String,List<JFun>>();
	public static Map<String,Map<String,String>> RULEPARSEDRL = new ConcurrentHashMap<String,Map<String,String>>();
	public static Map<String,List<DataSetTreeVo>> DATASETTREEVO = new ConcurrentHashMap<String,List<DataSetTreeVo>>();
	public static Map<String,Map<String, RuleDataset>> MAPRULEDATASET = new ConcurrentHashMap<String,Map<String, RuleDataset>>();
	public static Map<String,Map<String, MClass>> MCLASSCACHE = new ConcurrentHashMap<String,Map<String, MClass>>();
	public static Map<String,Map<String, RuleDataparams>> RULEDATAPARAMS_MAP = new ConcurrentHashMap<String,Map<String, RuleDataparams>>();
	public static Map<String,Map<String, List<RuleDataparams>>> RULEDATAPARAMS_LIST = new ConcurrentHashMap<String,Map<String,List<RuleDataparams>>>();
	public static Map<String,Collection<RuleReferenceData>> REFFUNCPARAM = new ConcurrentHashMap<String,Collection<RuleReferenceData>>();
	public static Map<String,List<RuleSetVO>> RULESETEFFECT = new ConcurrentHashMap<String,List<RuleSetVO>>();
	public static Map<String,Map<String,List<RuleRefVO>>> RULE_REF_ENTRY_GROUP = new ConcurrentHashMap<String,Map<String,List<RuleRefVO>>>();
	public static Map<String,List<RuleAuthority>> RULE_AUTH_MAP = null;
	public static List<RuleProject> RULE_PRO_LIST = null;
	public static Map<String,List<RuleProjectRank>> RULE_PRO_RANK_MAP = new ConcurrentHashMap<String,List<RuleProjectRank>>();
	
	/**
	 * 判断是否可以从缓存取数据
	 * @param projectCode
	 * @param option
	 * @return
	 */
	public static boolean canGetFromCache(String cacheKey) {
		String[] arr = cacheKey.split(SPLIT);
		String option = arr[1];
		Boolean contain = false;
		if(JFUNCACHE_OPTION.equals(option)) {
			contain = JFUNCACHE.containsKey(cacheKey);
		}else if(BOMCACHE_OPTION.equals(option)) {
			contain = BOMCACHE.containsKey(cacheKey);
		}else if(DOMAINMAP_OPTION.equals(option)) {
			contain = DOMAINMAP!=null;
		}else if(RULEPARSEDRL_OPTION.equals(option)) {
			contain = RULEPARSEDRL.containsKey(cacheKey);
		}else if(DATASETTREEVO_OPTION.equals(option)) {
			contain = DATASETTREEVO.containsKey(cacheKey);
		}else if(DOMAINCACHE_OPTION.equals(option)) {
			contain = DOMAINCACHE.containsKey(cacheKey);
		}else if(MAPRULEDATASET_OPTION.equals(option)) {
			contain = MAPRULEDATASET.containsKey(cacheKey);
		}else if(MCLASSCACHE_OPTION.equals(option)) {
			contain = MAPRULEDATASET.containsKey(cacheKey);
		}else if(RULEDATAPARAMS_MAP_OPTION.equals(option)) {
			contain = RULEDATAPARAMS_MAP.containsKey(cacheKey);
		}else if(RULEDATAPARAMS_LIST_OPTION.equals(option)) {
			contain = RULEDATAPARAMS_LIST.containsKey(cacheKey);
		}else if(REFFUNCPARAM_OPTION.equals(option)) {
			contain = REFFUNCPARAM.containsKey(cacheKey);
		}else if(RULESETEFFECT_OPTION.equals(option)) {
			contain = RULESETEFFECT.containsKey(cacheKey);
		}else if(RULE_REF_ENTRY_GROUP_OPTION.equals(option)) {
			contain = RULE_REF_ENTRY_GROUP.containsKey(cacheKey);
		}else if(RULE_AUTH_MAP_OPTION.equals(option)) {
			contain = RULE_AUTH_MAP!=null;
		}else if(RULE_PRO_LIST_OPTION.equals(option)) {
			contain = RULE_PRO_LIST!=null;
		}else if(RULE_PRO_RANK_MAP_OPTION.equals(option)) {
			contain = RULE_PRO_RANK_MAP.containsKey(cacheKey);
		}
    	return contain;
	}
	/**
	 * 获取存储的key
	 * @param projectCode
	 * @param option
	 * @return
	 */
	public static String getCacheKey(String projectCode,String option){
		return projectCode+SPLIT+option;
	}
	/**
	 * 函数变量发生改变
	 * @param projectCode
	 * @param value
	 */
	public static void funcParamChange(String projectCode) {
		JFUNCACHE.remove(getCacheKey(projectCode,JFUNCACHE_OPTION));
		RULEPARSEDRL.remove(getCacheKey(projectCode, RULEPARSEDRL_OPTION));
		System.out.println("-----DataCache函数变量缓存清理-----");
		paramChange(projectCode);
	}
	/**
	 * 函数变量关联参数改变，一般保存规则时执行
	 * @param projectCode
	 */
	public static void refFuncParamChange(String projectCode) {
		REFFUNCPARAM.remove(getCacheKey(projectCode, REFFUNCPARAM_OPTION));
		System.out.println("-----DataCache函数变量关联参数缓存清理-----");
	}
	/**
	 * 变量发生改变
	 * @param projectCode
	 * @param value
	 */
	public static void paramChange(String projectCode) {
		BOMCACHE.remove(getCacheKey(projectCode,BOMCACHE_OPTION));
		DOMAINCACHE.remove(getCacheKey(projectCode,DOMAINCACHE_OPTION));
		RULEPARSEDRL.remove(getCacheKey(projectCode, RULEPARSEDRL_OPTION));
		DATASETTREEVO.remove(getCacheKey(projectCode, DATASETTREEVO_OPTION));
		MAPRULEDATASET.remove(getCacheKey(projectCode, MAPRULEDATASET_OPTION));
		MCLASSCACHE.remove(getCacheKey(projectCode, MCLASSCACHE_OPTION));
		RULEDATAPARAMS_MAP.remove(getCacheKey(projectCode, RULEDATAPARAMS_MAP_OPTION));
		RULEDATAPARAMS_LIST.remove(getCacheKey(projectCode, RULEDATAPARAMS_LIST_OPTION));
		RULEDATAPARAMS_LIST.remove(getCacheKey("", DOMAINMAP_OPTION));
		System.out.println("-----DataCache数据模型变量缓存清理-----");
	}
	/**
	 * 枚举变量发生改变
	 * @param projectCode
	 * @param value
	 */
	public static void enumChange(String projectCode) {
		DOMAINMAP = null;
		DOMAINCACHE.remove(getCacheKey(projectCode, DOMAINCACHE_OPTION));
		System.out.println("-----DataCache枚举变量缓存清理-----");
	}
	/**
	 * 权限发生改变
	 * @param projectCode
	 * @param value
	 */
	public static void ruleAuthorityChange() {
		RULE_AUTH_MAP = null;
		System.out.println("-----DataCache权限缓存清理-----");
	}
	/**
	 * 项目发生改变
	 * @param projectCode
	 * @param value
	 */
	public static void ruleProjectChange(String projectCode) {
		RULE_PRO_LIST = null;
		System.out.println("-----DataCache项目缓存清理-----");
		ruleProjectRankChange(projectCode);
	}
	/**
	 * 项目目录发生改变
	 * @param projectCode
	 * @param value
	 */
	public static void ruleProjectRankChange(String projectCode) {
		RULE_PRO_LIST = null;
		RULE_PRO_RANK_MAP.remove(getCacheKey(projectCode, RULE_PRO_RANK_MAP_OPTION));
		System.out.println("-----DataCache项目目录缓存清理-----");
	}
	/**
	 * 生效规则改变
	 * @param projectCode
	 */
	public static void ruleSetEffectChange(String projectCode) {
		RULESETEFFECT.remove(getCacheKey(projectCode, RULESETEFFECT_OPTION));
		System.out.println("-----DataCache生效规则缓存清理-----");
	}
	/**
	 * 条目规则集改变
	 * @param projectCode
	 */
	public static void ruleRefEntryGroupChange(String projectCode) {
		RULE_REF_ENTRY_GROUP.remove(getCacheKey(projectCode, RULE_REF_ENTRY_GROUP_OPTION));
		System.out.println("-----DataCache条目规则集改变缓存清理-----");
	}
	
	public static void cleanAll() {
		BOMCACHE = new ConcurrentHashMap<String,Map<String, Bom>>();
		DOMAINCACHE = new ConcurrentHashMap<String,Map<String, Domain>>();
		DOMAINMAP = null; 
		JFUNCACHE = new ConcurrentHashMap<String,List<JFun>>();
		RULEPARSEDRL = new ConcurrentHashMap<String,Map<String,String>>();
		DATASETTREEVO = new ConcurrentHashMap<String,List<DataSetTreeVo>>();
		MAPRULEDATASET = new ConcurrentHashMap<String,Map<String, RuleDataset>>();
		MCLASSCACHE = new ConcurrentHashMap<String,Map<String, MClass>>();
		RULEDATAPARAMS_MAP = new ConcurrentHashMap<String,Map<String, RuleDataparams>>();
		RULEDATAPARAMS_LIST = new ConcurrentHashMap<String,Map<String,List<RuleDataparams>>>();
		REFFUNCPARAM = new ConcurrentHashMap<String,Collection<RuleReferenceData>>();
		RULESETEFFECT = new ConcurrentHashMap<String,List<RuleSetVO>>();
		RULE_REF_ENTRY_GROUP = new ConcurrentHashMap<String,Map<String,List<RuleRefVO>>>();
		RULE_AUTH_MAP = null;
		RULE_PRO_LIST = null;
		RULE_PRO_RANK_MAP = new ConcurrentHashMap<String,List<RuleProjectRank>>();
		System.out.println("-----DataCache全部缓存清理-----");
	}
	/**
	 * 冲缓存中取值
	 * @param cacheKey
	 * @return
	 */
	public static Object getValue(String cacheKey) {
		String[] arr = cacheKey.split(SPLIT);
		String option = arr[1];
		if(JFUNCACHE_OPTION.equals(option)) {
			return JFUNCACHE.get(cacheKey);
		}else if(BOMCACHE_OPTION.equals(option)) {
			return BOMCACHE.get(cacheKey);
		}else if(DOMAINMAP_OPTION.equals(option)) {
			return DOMAINMAP;
		}else if(RULEPARSEDRL_OPTION.equals(option)) {
			return RULEPARSEDRL.get(cacheKey);
		}else if(DATASETTREEVO_OPTION.equals(option)) {
			return DATASETTREEVO.get(cacheKey);
		}else if(DOMAINCACHE_OPTION.equals(option)) {
			return DOMAINCACHE.get(cacheKey);
		}else if(MAPRULEDATASET_OPTION.equals(option)) {
			return MAPRULEDATASET.get(cacheKey);
		}else if(MCLASSCACHE_OPTION.equals(option)) {
			return MCLASSCACHE.get(cacheKey);
		}else if(RULEDATAPARAMS_MAP_OPTION.equals(option)) {
			return RULEDATAPARAMS_MAP.get(cacheKey);
		}else if(RULEDATAPARAMS_LIST_OPTION.equals(option)) {
			return RULEDATAPARAMS_LIST.get(cacheKey);	
		}else if(REFFUNCPARAM_OPTION.equals(option)) {
			return REFFUNCPARAM.get(cacheKey);
		}else if(RULESETEFFECT_OPTION.equals(option)) {
			return RULESETEFFECT.get(cacheKey);
		}else if(RULE_REF_ENTRY_GROUP_OPTION.equals(option)) {
			return RULE_REF_ENTRY_GROUP.get(cacheKey);	
		}else if(RULE_AUTH_MAP_OPTION.equals(option)) {
			return RULE_AUTH_MAP;
		}else if(RULE_PRO_LIST_OPTION.equals(option)) {
			return RULE_PRO_LIST;
		}else if(RULE_PRO_RANK_MAP_OPTION.equals(option)) {
			return RULE_PRO_RANK_MAP.get(cacheKey);
		}else {
			throw new RuntimeException("不存在缓存");
		}
	}
	
	/**
	 * 赋值到缓存中
	 * @param cacheKey
	 * @param obj
	 */
	@SuppressWarnings("unchecked")
	public static void setValue(String cacheKey,Object obj) {
		String[] arr = cacheKey.split(SPLIT);
		String option = arr[1];
		if(JFUNCACHE_OPTION.equals(option)) {
			JFUNCACHE.put(cacheKey,(List<JFun>)obj);
		}else if(BOMCACHE_OPTION.equals(option)) {
			BOMCACHE.put(cacheKey,(Map<String, Bom>)obj);
		}else if(DOMAINMAP_OPTION.equals(option)) {
			DOMAINMAP = (Map<String, List<RuleDataDomain>>) obj;
		}else if(RULEPARSEDRL_OPTION.equals(option)) {
			RULEPARSEDRL.put(cacheKey,(Map<String, String>) obj);
		}else if(DATASETTREEVO_OPTION.equals(option)) {
			DATASETTREEVO.put(cacheKey,(List<DataSetTreeVo>) obj);
		}else if(DOMAINCACHE_OPTION.equals(option)) {
			DOMAINCACHE.put(cacheKey,(Map<String, Domain>) obj);
		}else if(MAPRULEDATASET_OPTION.equals(option)) {
			MAPRULEDATASET.put(cacheKey,(Map<String, RuleDataset>) obj);
		}else if(MCLASSCACHE_OPTION.equals(option)) {
			MCLASSCACHE.put(cacheKey,(Map<String, MClass>) obj);
		}else if(RULEDATAPARAMS_MAP_OPTION.equals(option)) {
			RULEDATAPARAMS_MAP.put(cacheKey,(Map<String, RuleDataparams>) obj);
		}else if(RULEDATAPARAMS_LIST_OPTION.equals(option)) {
			RULEDATAPARAMS_LIST.put(cacheKey,(Map<String, List<RuleDataparams>>) obj);
		}else if(REFFUNCPARAM_OPTION.equals(option)) {
			REFFUNCPARAM.put(cacheKey,(Collection<RuleReferenceData>) obj);
		}else if(RULESETEFFECT_OPTION.equals(option)) {
			RULESETEFFECT.put(cacheKey,(List<RuleSetVO>) obj);
		}else if(RULE_REF_ENTRY_GROUP_OPTION.equals(option)) {
			RULE_REF_ENTRY_GROUP.put(cacheKey,(Map<String,List<RuleRefVO>>) obj);
		}else if(RULE_AUTH_MAP_OPTION.equals(option)) {
			RULE_AUTH_MAP = (Map<String, List<RuleAuthority>>) obj;
		}else if(RULE_PRO_LIST_OPTION.equals(option)) {
			RULE_PRO_LIST =(List<RuleProject>) obj;
		}else if(RULE_PRO_RANK_MAP_OPTION.equals(option)) {
			RULE_PRO_RANK_MAP.put(cacheKey,(List<RuleProjectRank>) obj);
		}else {
			throw new RuntimeException("不存在缓存");
		}
	}
}
